// pages/editMonth/editMonth.js
import nami from '../../nami/index';
import utils from '../../utils/utils';

var ID = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text: '',
    diary: [],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      nami.request({
        url: 'http://localhost:8080/request/diary/showMonth.groovy',
        data: {
          date: new Date().format('yyyy-MM-dd')
        },
        success: function (res) {
          if (!res.data) {
            nami.request({
              url: 'http://localhost:8080/request/diary/editMonth.groovy',
              success: function (res) {
                  console.log('id:',res.data);
                  ID = res.data.ID;
                }
            });
          } else {
            console.log(res.data);
            if (res.data.content.CONTENT != null){
              that.setData({
                text: res.data.content.CONTENT
              }); 
            } 
            ID = res.data.id.ID;
          }
        }
      });
      nami.request({
        url: '/request/diary/showWeek.groovy',
        data: {
          month: new Date().getMonth() + 1,
          year: new Date().getFullYear()
        },
        success: function (res) {
          console.log('This month diary: ',res.data);
          that.setData({
            diary: res.data
          });
        }
      });
  
  },

  formSubmit: function(e) {
    var that = this;
    var content = e.detail.value.content;
    nami.request({
      url: 'http://localhost:8080/request/diary/editMonth.groovy',
      data: {
        ID: ID,
        content: content
      },
      success: function (res) {
        console.log(res);
        if (res.data) {
          wx.navigateBack();
        } else {
          console.log('跳转失败');
        }
      }
    });
  }
})